```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx反向代理缓存配置指南 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #2d3748;
            color: #e2e8f0;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .code-header {
            background-color: #1a202c;
            padding: 0.5rem 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: '';
            position: absolute;
            bottom: 0.1em;
            left: 0;
            right: 0;
            height: 0.4em;
            background-color: rgba(118, 75, 162, 0.3);
            z-index: -1;
            transform: skew(-15deg);
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 md:py-32 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Nginx反向代理缓存<br><span class="highlight">配置与优化指南</span></h1>
                    <p class="text-xl text-gray-100 mb-8">提升网站性能的关键技术：从基础配置到高级优化策略</p>
                    <div class="flex space-x-4">
                        <a href="#basic-config" class="bg-white text-indigo-700 px-6 py-3 rounded-lg font-medium hover:bg-gray-100 transition duration-300">
                            <i class="fas fa-code mr-2"></i>基础配置
                        </a>
                        <a href="#optimization" class="bg-indigo-800 bg-opacity-30 border border-white text-white px-6 py-3 rounded-lg font-medium hover:bg-opacity-50 transition duration-300">
                            <i class="fas fa-rocket mr-2"></i>优化策略
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-10 p-8 rounded-xl backdrop-filter backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                                A[用户请求] --> B{Nginx缓存}
                                B -->|缓存命中| C[返回缓存响应]
                                B -->|缓存未命中| D[后端服务器]
                                D --> E[返回响应并缓存]
                                C --> F[快速响应]
                                E --> F
                                G[缓存管理] -->|清理| B
                                G -->|更新| B
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-16">
        <!-- Introduction -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 rounded-full w-12"></div>
                <h2 class="ml-4 text-2xl font-semibold text-gray-800">核心概念</h2>
            </div>
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="p-8">
                    <h3 class="text-xl font-semibold text-gray-800 mb-4">什么是反向代理缓存？</h3>
                    <p class="text-gray-600 mb-6">反向代理缓存是一种将请求的响应缓存到 Nginx 服务器上的机制，以减少后端服务器的负担，提高响应速度和降低延迟。Nginx 作为反向代理服务器，可以缓存从后端服务器获取的响应，并在后续相同请求时直接从缓存中提供响应。</p>
                    <div class="flex flex-wrap gap-4">
                        <div class="flex items-center bg-indigo-50 text-indigo-700 px-4 py-2 rounded-full">
                            <i class="fas fa-bolt mr-2"></i>
                            <span>提升响应速度</span>
                        </div>
                        <div class="flex items-center bg-indigo-50 text-indigo-700 px-4 py-2 rounded-full">
                            <i class="fas fa-server mr-2"></i>
                            <span>减轻后端负载</span>
                        </div>
                        <div class="flex items-center bg-indigo-50 text-indigo-700 px-4 py-2 rounded-full">
                            <i class="fas fa-shield-alt mr-2"></i>
                            <span>增强可用性</span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Basic Configuration -->
        <section id="basic-config" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 rounded-full w-12"></div>
                <h2 class="ml-4 text-2xl font-semibold text-gray-800">基础配置</h2>
            </div>
            
            <!-- Cache Zone Definition -->
            <div class="mb-12">
                <h3 class="text-xl font-semibold text-gray-800 mb-6 flex items-center">
                    <span class="w-8 h-8 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3">1</span>
                    定义缓存区域
                </h3>
                <p class="text-gray-600 mb-6">首先，定义缓存区域，这个区域决定了缓存数据的存储位置和大小。</p>
                
                <div class="code-block mb-6">
                    <div class="code-header">
                        <span class="text-sm font-mono text-gray-400">nginx.conf</span>
                        <button class="text-gray-400 hover:text-white">
                            <i class="far fa-copy"></i>
                        </button>
                    </div>
                    <pre class="p-4 overflow-x-auto"><code>http {
    # 定义缓存区域
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
}</code></pre>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <h4 class="font-medium text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-info-circle text-indigo-500 mr-2"></i>
                            参数说明
                        </h4>
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li><span class="font-medium">/var/cache/nginx</span> - 缓存数据存储的位置</li>
                            <li><span class="font-medium">levels=1:2</span> - 缓存目录的层级结构</li>
                            <li><span class="font-medium">keys_zone=my_cache:10m</span> - 缓存区域的名字和内存大小</li>
                            <li><span class="font-medium">max_size=1g</span> - 最大缓存大小</li>
                            <li><span class="font-medium">inactive=60m</span> - 缓存项过期时间</li>
                        </ul>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <h4 class="font-medium text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-lightbulb text-indigo-500 mr-2"></i>
                            最佳实践
                        </h4>
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li>根据服务器内存设置合理的 <code>keys_zone</code> 大小</li>
                            <li>对于大型网站，考虑使用独立的硬盘分区存储缓存</li>
                            <li>根据内容更新频率设置 <code>inactive</code> 时间</li>
                            <li>生产环境建议关闭 <code>use_temp_path</code> 以提升性能</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <!-- Cache Behavior Configuration -->
            <div class="mb-12">
                <h3 class="text-xl font-semibold text-gray-800 mb-6 flex items-center">
                    <span class="w-8 h-8 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3">2</span>
                    配置缓存行为
                </h3>
                <p class="text-gray-600 mb-6">在 <code>server</code> 或 <code>location</code> 块中配置缓存行为。</p>
                
                <div class="code-block mb-6">
                    <div class="code-header">
                        <span class="text-sm font-mono text-gray-400">nginx.conf</span>
                        <button class="text-gray-400 hover:text-white">
                            <i class="far fa-copy"></i>
                        </button>
                    </div>
                    <pre class="p-4 overflow-x-auto"><code>server {
    listen 80;
    server_name demo.com;

    location / {
        proxy_pass http://backend;
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_cache_use_stale error timeout updating;
        proxy_cache_revalidate on;
    }
}</code></pre>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <h4 class="font-medium text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-cog text-indigo-500 mr-2"></i>
                            指令解析
                        </h4>
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li><span class="font-medium">proxy_pass</span> - 后端服务器地址</li>
                            <li><span class="font-medium">proxy_cache</span> - 使用的缓存区域</li>
                            <li><span class="font-medium">proxy_cache_valid</span> - 不同状态码的缓存时间</li>
                            <li><span class="font-medium">proxy_cache_use_stale</span> - 特殊情况使用过期缓存</li>
                            <li><span class="font-medium">proxy_cache_revalidate</span> - 启用缓存重新验证</li>
                        </ul>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-sm">
                        <h4 class="font-medium text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-exclamation-triangle text-indigo-500 mr-2"></i>
                            注意事项
                        </h4>
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li>确保后端设置了适当的 <code>Cache-Control</code> 头部</li>
                            <li>对于个性化内容，避免缓存或设置较短的有效期</li>
                            <li>404 等错误状态的缓存时间应设置较短</li>
                            <li>测试环境中可以先设置较短的缓存时间</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Optimization Strategies -->
        <section id="optimization" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 rounded-full w-12"></div>
                <h2 class="ml-4 text-2xl font-semibold text-gray-800">优化策略</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Cache Granularity -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-8">
                        <div class="w-12 h-12 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-filter text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">缓存粒度控制</h3>
                        <p class="text-gray-600 mb-6">根据实际需求设置缓存粒度，比如缓存特定的 URL、请求方法等。</p>
                        
                        <div class="code-block mb-6">
                            <div class="code-header">
                                <span class="text-sm font-mono text-gray-400">nginx.conf</span>
                                <button class="text-gray-400 hover:text-white">
                                    <i class="far fa-copy"></i>
                                </button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>location /api/ {
    proxy_cache my_cache;
    proxy_cache_valid 200 5m;
    proxy_cache_valid 500 1m;
    proxy_cache_key "$scheme$request_method$host$request_uri";
}</code></pre>
                        </div>
                        
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li><span class="font-medium">proxy_cache_key</span> - 定义缓存键值，包含协议、请求方法、主机和URI</li>
                            <li>API接口建议设置较短的缓存时间</li>
                            <li>不同HTTP方法(GET/POST等)应区分缓存</li>
                        </ul>
                    </div>
                </div>
                
                <!-- Cache Headers -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-8">
                        <div class="w-12 h-12 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-heading text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">缓存头设置</h3>
                        <p class="text-gray-600 mb-6">利用HTTP缓存头来控制缓存行为，同时添加自定义头部显示缓存状态。</p>
                        
                        <div class="code-block mb-6">
                            <div class="code-header">
                                <span class="text-sm font-mono text-gray-400">nginx.conf</span>
                                <button class="text-gray-400 hover:text-white">
                                    <i class="far fa-copy"></i>
                                </button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>location / {
    proxy_pass http://backend;
    proxy_cache my_cache;
    proxy_cache_valid 200 10m;
    add_header X-Cache-Status $upstream_cache_status;
}</code></pre>
                        </div>
                        
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li><span class="font-medium">X-Cache-Status</span> - 显示缓存状态(MISS/HIT/BYPASS)</li>
                            <li>配合后端应用的<code>Cache-Control</code>头部</li>
                            <li>调试时可添加多些调试头信息</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Cache Invalidation -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 rounded-full w-12"></div>
                <h2 class="ml-4 text-2xl font-semibold text-gray-800">缓存失效机制</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Active Invalidation -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-8">
                        <div class="w-12 h-12 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-sync-alt text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">主动失效</h3>
                        <p class="text-gray-600 mb-6">通过特殊请求强制清除特定缓存内容，需要安装<code>ngx_cache_purge</code>模块。</p>
                        
                        <div class="code-block mb-6">
                            <div class="code-header">
                                <span class="text-sm font-mono text-gray-400">nginx.conf</span>
                                <button class="text-gray-400 hover:text-white">
                                    <i class="far fa-copy"></i>
                                </button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>location /purge/ {
    proxy_cache_purge my_cache "$scheme$request_method$host$request_uri";
}</code></pre>
                        </div>
                        
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li>通过访问<code>/purge/</code>路径清除缓存</li>
                            <li>需要严格控制访问权限</li>
                            <li>建议结合认证机制使用</li>
                        </ul>
                    </div>
                </div>
                
                <!-- Automatic Cleanup -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-8">
                        <div class="w-12 h-12 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-broom text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">自动清理</h3>
                        <p class="text-gray-600 mb-6">基于时间和空间限制自动清理过期和未使用的缓存。</p>
                        
                        <div class="code-block mb-6">
                            <div class="code-header">
                                <span class="text-sm font-mono text-gray-400">nginx.conf</span>
                                <button class="text-gray-400 hover:text-white">
                                    <i class="far fa-copy"></i>
                                </button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>proxy_cache_path /var/cache/nginx 
    levels=1:2 
    keys_zone=my_cache:10m 
    max_size=1g 
    inactive=60m 
    use_temp_path=off;</code></pre>
                        </div>
                        
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li><span class="font-medium">max_size</span> - 限制缓存总大小</li>
                            <li><span class="font-medium">inactive</span> - 未访问缓存的保留时间</li>
                            <li>Nginx会定期清理过期缓存</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Advanced Features -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 rounded-full w-12"></div>
                <h2 class="ml-4 text-2xl font-semibold text-gray-800">高级功能</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Tiered Caching -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-8">
                        <div class="w-12 h-12 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-layer-group text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">分层缓存</h3>
                        <p class="text-gray-600 mb-6">通过多级缓存架构提升缓存效率和可靠性。</p>
                        
                        <div class="mermaid mb-6">
                            graph LR
                                A[客户端] --> B[边缘节点缓存]
                                B -->|未命中| C[中心节点缓存]
                                C -->|未命中| D[源服务器]
                        </div>
                        
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li>边缘节点缓存高频访问内容</li>
                            <li>中心节点缓存全量内容</li>
                            <li>减少回源请求</li>
                        </ul>
                    </div>
                </div>
                
                <!-- Cache Compression -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                    <div class="p-8">
                        <div class="w-12 h-12 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-compress-alt text-xl"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-gray-800 mb-4">缓存压缩</h3>
                        <p class="text-gray-600 mb-6">启用缓存内容压缩以节省存储空间，权衡CPU消耗。</p>
                        
                        <div class="code-block mb-6">
                            <div class="code-header">
                                <span class="text-sm font-mono text-gray-400">nginx.conf</span>
                                <button class="text-gray-400 hover:text-white">
                                    <i class="far fa-copy"></i>
                                </button>
                            </div>
                            <pre class="p-4 overflow-x-auto"><code>http {
    gzip on;
    gzip_types text/plain application/json;
}</code></pre>
                        </div>
                        
                        <ul class="space-y-2 text-gray-600 text-sm">
                            <li>对文本内容压缩效果显著</li>
                            <li>图像/视频等已压缩内容不需要再次压缩</li>
                            <li>根据服务器性能调整压缩级别</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="bg-indigo-50 rounded-xl p-8 md:p-12 mb-20">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-2/3 mb-6 md:mb-0">
                    <h2 class="text-2xl font-semibold text-gray-800 mb-4">提升缓存效果的关键要点</h2>
                    <p class="text-gray-600">合理配置Nginx反向代理缓存可以显著提升网站性能，但需要根据实际业务需求不断调整和优化。监测缓存命中率，定期审核缓存策略，才能充分发挥缓存的价值。</p>
                </div>
                <div class="md:w-1/3 flex justify-center">
                    <div class="bg-white p-6 rounded-lg shadow-md w-full max-w-xs">
                        <h3 class="font-semibold text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-check-circle text-indigo-500 mr-2"></i>
                            检查清单
                        </h3>
                        <ul class="space-y-2 text-sm text-gray-600">
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>缓存区域大小设置合理</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>不同状态码缓存时间适当</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>缓存键设计合理</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>有缓存失效机制</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-green-500 mt-1 mr-2"></i>
                                <span>监控缓存命中率</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-semibold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">专业的Web技术资源中心</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-sm text-gray-500 text-center">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 复制代码功能
        document.querySelectorAll('.code-block button').forEach(button => {
            button.addEventListener('click', () => {
                const code = button.parentElement.nextElementSibling.textContent;
                navigator.clipboard.writeText(code).then(() => {
                    const originalIcon = button.innerHTML;
                    button.innerHTML = '<i class="fas fa-check"></i>';
                    setTimeout(() => {
                        button.innerHTML = originalIcon;
                    }, 2000);
                });
            });
        });
    </script>
</body>
</html>
```